package com.zh.work.controller.platform.branch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.work.dao.platform.branch.BranchRoleMapper;
import com.zh.work.entity.branch.BranchRoleEntity;
import com.zh.work.service.platform.branch.BranchRoleService;
import com.zh.common.entity.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 机构角色管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/8/26
 * @Version: 1.0.1
 */
@Api(value="BranchRoleController", tags = "机构角色管理控制器")
@RestController
@RequestMapping("/branchRole")
public class BranchRoleController {


    @Autowired
    private BranchRoleService service;

    @Autowired
    private BranchRoleMapper mapper;

    @ApiOperation(value = "分页查询机构角色数据集", tags = {"getPagesBranchRoles"}, notes = "分页查询机构角色数据集")
    @GetMapping("/getPagesBranchRoles")
    public JSONResult<List<BranchRoleEntity>> getPagesBranchRoles(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "显示数量页数", name = "pageNo")Integer pageSize,
            @ApiParam(value = "机构角色实体类", name = "branchRoleEntity") BranchRoleEntity branchRoleEntity) {
        JSONResult<List<BranchRoleEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<BranchRoleEntity> page = this.mapper.selectPage(
                    new Page<>(pageNo, pageSize),
                    new QueryWrapper<BranchRoleEntity>()
                            .eq(Objects.nonNull(branchRoleEntity.getId()), "id", branchRoleEntity.getId())
                            .likeLeft(StringUtils.isNotEmpty(branchRoleEntity.getRolerName()),"roler_name", branchRoleEntity.getRolerName())
                            .likeLeft(StringUtils.isNotEmpty(branchRoleEntity.getRolerMemo()),"roler_memo", branchRoleEntity.getRolerMemo())
            );
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(page.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModBranchRoleById"}, notes = "通过id修改信息,请使用application/json格式提交哦!")
    @PutMapping("/putModBranchRoleById")
    @ResponseBody
    public JSONResult<Object> putModBranchRoleById(@ApiParam(value = "机构角色实体类", name = "branchRoleEntity")@RequestBody BranchRoleEntity branchRoleEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (branchRoleEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


   /* @ApiOperation(value = "根据主键删除记录", tags = {"deleteBranchRoleById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteBranchRoleById")
    public JSONResult<Object> deleteInfoById(@ApiParam(value = "机构角色实体类", name = "BranchInfo") BranchRoleEntity branchRoleEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (branchRoleEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }*/



   @ApiOperation(value = "", tags = {""}, notes = "")
    public JSONResult<BranchRoleEntity> updateBranchRoleAuth(){
       return null;
   }


    @ApiOperation(value = "根据id查询详细信息", tags = {"getBranchRoleById"}, notes = "根据id查询详细信息")
    @GetMapping("/getBranchRoleById")
    public JSONResult<BranchRoleEntity> getBranchRoleById(@ApiParam(value = "机构角色实体类", name = "branchRoleEntity") BranchRoleEntity branchRoleEntity) {
        JSONResult<BranchRoleEntity> jsonResult = new JSONResult<>();
        try {
            BranchRoleEntity result = branchRoleEntity.selectById(branchRoleEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

}
